<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .box{
            width: 100px;height: 100px;background: pink;position: absolute;left: 0;top: 0;
        }
        .line{
            width: 2px;height: 500px;background: blue;position: absolute;left: 500px;top: 0;
        }
    </style>
</head>
<body>
    <div class="box"></div>
    <div class="line"></div>
</body>
<script>
    const obox=document.querySelector(".box");
    // 定义计时器
    let timer;
    // 设定步长
    let speed=2;
    // 设定目标值
    const target=400;

    obox.onclick=function(){
        clearInterval(timer);
        timer=setInterval(function(){
            if(target - obox.offsetLeft < speed){
                clearInterval(timer);
                obox.style.left=target + "px";
            }else{
                obox.style.left=obox.offsetLeft + speed +"px";
            }
        },30)
    }



</script>
</html>